package com.base.control;

import com.base.exception.IMError;
import com.base.exception.ParamFormatException;
import com.base.util.StringUtil;

/**
 * 检查器接口
 */


/**
 * Created by yjh on 15-3-10.
 */
public class TextParamCheck {

    public static interface TextParamChecker {
        boolean check(String text);
    }

    //常用检查器
    public static TextParamChecker PHONE_NUMBER_CHECKER = new TextParamChecker() {
        @Override
        public boolean check(String text) {
            return text.matches("\\d{11}");
        }
    };

    public static TextParamChecker PASSWORD_CHECKER = new TextParamChecker() {
        @Override
        public boolean check(String text) {
            return text.matches("[@A-Za-z\\d!#\\$%\\^\\*\\.~]{4,20}");
        }
    };

    public static TextParamChecker EMAIL_CHECKER = new TextParamChecker() {
        @Override
        public boolean check(String text) {
            return text.matches("^\\s*\\w+(?:\\.?[\\w-]+)*@[a-zA-Z0-9]+(?:[-.][a-zA-Z0-9]+)*\\.[a-zA-Z]+\\s*$");
        }
    };

    public static TextParamChecker VERIFY_CHECKER = new TextParamChecker() {
        @Override
        public boolean check(String text) {
            return text.matches("\\d+");
        }
    };

    public static TextParamChecker EMPTY_CHECKER = new TextParamChecker() {
        @Override
        public boolean check(String text) {
            return true;
        }
    };

    public static TextParamChecker SEARCH_KEY_CHECKER = new TextParamChecker() {
        @Override
        public boolean check(String text) {
            return text.matches(".{1,50}");
        }
    };

    public static TextParamChecker NICKNAME_CHECKER = new TextParamChecker() {
        @Override
        public boolean check(String text) {
            return text.matches(".{1,10}");
        }
    };

    public static TextParamChecker CONTACT_CONTENT_CHECKER = new TextParamChecker() {
        @Override
        public boolean check(String text) {
            return text.matches("(?s).{1,300}");
        }
    };

    public static TextParamChecker QQ_CHECKER = new TextParamChecker() {
        @Override
        public boolean check(String text) {
            return text.matches("\\d{6,11}");
        }
    };

    public static TextParamChecker CONTACT_METHOD_CHECKER = new TextParamChecker() {
        @Override
        public boolean check(String text) {
            return PHONE_NUMBER_CHECKER.check(text) || EMAIL_CHECKER.check(text) || QQ_CHECKER.check(text);
        }
    };

    public static TextParamChecker LOGIN_ACCOUNT_CHECKER = new TextParamChecker() {
        @Override
        public boolean check(String text) {
            return PHONE_NUMBER_CHECKER.check(text) || text.matches("(?i)(gs|ydd)\\d{7,10}");
        }
    };

    /**
     * 检查TextView及其子类text是否符合指定要求，不符合时通过toast抛出指定反馈信息
     * @param text
     * @param checker
     * @param emptyHint
     * @param errorHint
     * @return 辨明是否传递
     */
    public static boolean check(String text, IMError emptyHint,
                                IMError errorHint, TextParamChecker checker) throws ParamFormatException {
        boolean flag = false;
        text = text.trim();
        //0: normal; 1: empty; 2: error
        if(StringUtil.isEmpty(text)) {
            throw new ParamFormatException(emptyHint.getMsg());
        } else if(!checker.check(text)) {
            throw new ParamFormatException(errorHint.getMsg());
        } else {
            flag = true;
        }
        return flag;
    }


}
